<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
	<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
	<script src="vue.js"></script>
</head>
<body>
	<div class="container">
		<div id="editMsg">
			<form role="form">
				<div class="form-group">
					<label for="username">姓名</label>
					<input type="text" class="form-control" id="uesrname" v-model="username" placeholder="请输入姓名">
					<p v-model="username">{{username}}</p>
				</div>
				<div class="form-group">
					<label for="age">年龄</label>
					<input type="text" class="form-control" id="age" v-model="age" placeholder="请输入年龄">
				</div>
				<div class="text-right">
					<input class="btn btn-danger" type="reset" v-on:click="reset" value="重置">
					<input class="btn btn-info" type="button" v-on:click="add" value="添加">
				</div>
			</form>
			<table class="table table-hover table-bordered">
				<tbody class="text-center">
					<tr>
						<th class="text-center">编号</th>
						<th class="text-center">姓名</th>
						<th class="text-center">年龄</th>
						<th class="text-center">操作</th>
					</tr>
					<tr v-for="(item,index) in items">
						<td class="text-center">{{index+1}}</td>
						<td class="text-center">{{item.username}}</td>
						<td class="text-center">{{item.age}}</td>
						<td class="text-center"> 
							<button class="btn btn-sm btn-danger" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=index">删除</button>
						</td>
					</tr>
					<tr>
						<td colspan="4" class="warning text-right" v-show="items.length!=0">
							<button class="btn btn-danger" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=-2">全部删除</button>
						</td>
					</tr>
					<tr>
						<td colspan="4" class="warning" v-show="items.length==0">No Message</td>
					</tr>
				</tbody>
			</table>
			<div role="dialog" class="modal fade" id="layer">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button  class="close" data-dismiss="modal"> <span>&times;</span> </button>
							<div class="modal-title">确认删除吗？</div>
						</div>
						<div class="modal-body text-right">
							<button class="btn btn-primary"  data-dismiss="modal">取消</button>
							<button class="btn btn-danger" data-dismiss="modal" v-on:click="agree(nowIndex)">确认</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<script>
		var app =new Vue({
			el:"#editMsg",
			data:{
				username:'',
				age:'',
				nowIndex:-100,
				items:[ 
					{username:'leo',age:20},
					// {username:'blue',age:'18'},
				]
			},
			methods:{
				add:function () {
					if(this.username=='' || this.age==''){
						alert("请输入信息");
						return false;
					}else{
						this.items.push({
							username:this.username,
							age:this.age,
						});
						this.username='';
						this.age='' ;
					}
				},
				reset:function(){
					this.username='';
					this.age='';
				},
				agree:function (index) {
					if(index==-2){
						this.items=[];
					}else{
						this.items.splice(index,1);
					}
				}
			}
		})
	</script>
</body>
</html> 